package com.tanhua.server.controller;

import com.tanhua.common.util.UserThreadLocal;
import com.tanhua.server.service.TodayBestService;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/tanhua")
@Slf4j
public class TodayBestController {
    @Autowired
    private TodayBestService todayBestService;
    @Autowired
    private KeyGenerator keyGenerator;

    @GetMapping("/todayBest")
    public ResponseEntity<TodayBest> queryTodayBest() {
        try {
            TodayBest todayBest = todayBestService.queryTodayBest();
            if (null != todayBest) {
                return ResponseEntity.ok(todayBest);
            }
        } catch (Exception e) {
            log.error("查询今日佳人出错~ userId = " + UserThreadLocal.get().getId(), e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }


    /**
     * 推荐列表
     *
     * @param recommendUserQueryParam
     * @return
     */
    @GetMapping("/recommendation")
//    @Cacheable(cacheNames = "queryRecommendation")  //对于要使用Cacheable注解的方法,必须要返回值实现了Serializable接口,这里无法使用;使用详情见下面被注释的方法
    public ResponseEntity<PageResult> queryRecommendation(RecommendUserQueryParam recommendUserQueryParam) {
        try {
            PageResult pageResult = this.todayBestService.queryRecommendation(recommendUserQueryParam);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            log.error("查询推荐用户列表出错~ userId = " + UserThreadLocal.get().getId(), e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /*@GetMapping("/recommendation")
    //TODO 使用Cacheable进行redis缓存,应该是缓存成功了;但是找不到key,value存在哪里???
    @Cacheable(cacheNames = "queryRecommendation")
    public PageResult queryRecommendation(@RequestHeader("Authorization") String token, RecommendUserQueryParam recommendUserQueryParam) {
        try {
            //第一次查询,会进入方法并打印出aaa
            //第二次查询就不会进入方法了,会去MyCachingConfigurer中生成redis的key,但是redis中找不到key...
//            System.out.println("aaa");
            return this.todayBestService.queryRecommendation(token, recommendUserQueryParam);
        } catch (Exception e) {
            log.error("查询推荐用户列表出错 ~ token = " + token, e);
        }
        return null;
    }*/
}
